package com.vmix.android.vmixsdk.rds;

import java.io.File;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.util.Log;

import com.vmix.android.vmixsdk.config.VmixConfig;

/**
 * Upload files to VMIX via HTTP POST.  This operation will provide feedback on percent sent.
 * 
 * @author lei
 *
 */
public class FileUploadOperation extends AbstractOperation{

	protected String uploadToken;
	
	private String filePath;
	private String title;
	private String author;
	
	// constructor
	public FileUploadOperation(String filePath, String title, String author) {
		
		this.serviceUrl = VmixConfig.UPLOAD;
	
		this.uploadToken = VmixConfig.UTOKEN;
		
		// set up the rest of the params
		this.filePath = filePath;
		this.title = title;
		this.author = author;
		
	}
	
	/**
	 * 
	 * @param filePath - The absolute path of the file to be uploaded
	 * @param title
	 * @param author
	 */
	public String process() throws Exception {
	
		Boolean uploadStatus = false;
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		
		// using apache http client for better post handling
		DefaultHttpClient httpClient = new DefaultHttpClient();
		
		try {
			
			File file = new File( filePath );
			
			// generate a post action
			HttpPost httpPost = new HttpPost( this.serviceUrl );
			
			// get multipart entity
			MultipartEntity entity = new MultipartEntity();
			
			// add the input params as parts of input
			entity.addPart("token", new StringBody( this.uploadToken ) );
			entity.addPart("title", new StringBody( title ) );
			entity.addPart("author", new StringBody( author) );
			// add file
			entity.addPart("file_upload", new FileBody(file) );
			
			httpPost.setEntity( entity );
			
			HttpResponse response = httpClient.execute( httpPost );
		
			HttpEntity resEntity = response.getEntity();
			
			// parse the response into string
			InputStream inputStream = resEntity.getContent();
			
			DocumentBuilder builder = factory.newDocumentBuilder();
			
			Document dom = builder.parse( inputStream );
			
			Element root = dom.getDocumentElement();
			
			NodeList items = root.getElementsByTagName("media_id");
			
			// we should only have one, so get the first one
			Node mediaIdItem = items.item(0);
			
			if( mediaIdItem != null ) {
				// return the value
				return mediaIdItem.getFirstChild().getNodeValue();
			}
			
			return null;
			
		}
		catch( Exception e ) {
		
			Log.d("UPLOAD", e.getMessage() );

			throw e;
			
		}
		
	}
	
}
